package com.lwl.blog.controller;

import com.alibaba.fastjson.JSON;
import com.lwl.blog.core.constants.BaseConstants;
import com.lwl.blog.core.servlet.BaseServlet;
import com.lwl.blog.dto.EmailDto;
import com.lwl.blog.entity.UserEntity;
import com.lwl.blog.service.UserService;
import com.lwl.blog.service.impl.UserServiceImpl;
import com.lwl.blog.utils.EmailUtils;

import javax.servlet.annotation.WebServlet;
import java.io.IOException;
import java.sql.SQLException;

/**
 * 帖子用户响应层
 */
@WebServlet("/UserServlet/*")
public class UserServlet extends BaseServlet {
    //调用userservice层
    private UserService userService = new UserServiceImpl();

    //用户登录的方法
    public String login() throws IOException, SQLException {
        //1.接受前端json数据
        String s = req.getReader().readLine();
        //2.将前端数据转换成java数据，存入对象中
        UserEntity u = JSON.parseObject(s, UserEntity.class);
        //3.调用service层，让service处理login账号密码的比对
        u = userService.login(u.getUsername(), u.getPassword());
        System.out.println(u);
        //4.判断数据是否为空，登录成功
        if (u != null) {
            //5.密码作为敏感地段，需要将其设置为null
            u.setPassword(null);
            //6.将其放到绘画对象中
            req.getSession().setAttribute("loginUser", u);
            System.out.println("session");
            System.out.println("session"+req.getSession().getAttribute("loginUser"));
            return outJson(200, "登录成功！");
        }else {
            return outJson(100, "用户账号或密码错误！");
        }

    }

    public String register() throws Exception {
        //1.接收前端传过来的数据
        String s = req.getReader().readLine();
        //2.将前端传过来的数据转
        UserEntity u = JSON.parseObject(s, UserEntity.class);
        u.setPassword(u.getPassword());

        System.out.println(u);
        //3.调用业务层注册方法
        String register = userService.register(u);
        if (!"注册成功".equals(register)){
            return outJson(100,register);
        }
        String activationCode = u.getActivationCode();

        EmailDto e=new EmailDto();
        e.setTilte("论坛用户注册成功！");
        e.setToUser(u.getEmail());//设置邮箱
//        e.setContent("点击激活");
        e.setContent("<a href = 'http://127.0.0.1:8080/UserServlet/activation?code=" + activationCode + "'>点击激活</a>");

        //调用邮箱工具类转发
        EmailUtils.sendEmailInfoUser(e);

        //4.outjson做提示
        return outJson(true);
    }

    public String activation() throws Exception {
        //1.获得激活码
        String code = req.getParameter("code");
        System.out.println("code:"+code);
        //2.比对激活码
        UserEntity u=userService.selectActCode(code);
        System.out.println("UserServlet.activation："+u);
        //判断是否为空
        if (u!=null){
            //修改状态码
            u.setStatus(1);
            userService.update(u);

            return BaseConstants.BASE_REDIRECT+":"+"PageServlet/view/site/login";
        }else {
            return BaseConstants.BASE_SCRIPT+":"+"激活码有误!";
        }
    }
    //注销
    public void logout() throws Exception {
        req.getSession().invalidate();
        outView("view/site/login", null);
    }
}
